import Vue from 'vue'
import uniRequest from 'uni-request'

// create an axios instance
// if (process.env.NODE_ENV === 'development') {
	// uniRequest.defaults.baseURL = 'http://192.168.1.67:3002/';
// } else {
	// TODO 正式环境地址
	uniRequest.defaults.baseURL = 'https://cy.yebbt.cn';
	//uniRequest.defaults.baseURL = 'https://api.changyousudi.com';
// }
uniRequest.defaults.headers.patch['Content-Type'] = 'application/x-www-form-urlencoded'


uniRequest.interceptors.request.use(
	request => {
		return Promise.resolve(request)
		return request;
	},
	err => {
		console.log('请求失败');
		return Promise.reject(err);
	});
 
// 响应拦截
uniRequest.interceptors.response.use(function(res) {
	if (res.status === 500) {
		uni.showModal({
			title: '提示',
			content: '请求失败，请退出小程序重试',
			showCancel: false
		})
	} else {
		if (res.data.code === 200) {
			return Promise.resolve(res.data);
		} else if (res.data.error === 'ACCOUNT_FREEZE'){
			uni.showModal({
				title: '提示',
				content: res.data.msg,
				showCancel:false,
				success: res => {
					if (res.confirm) {
						console.log(res)
						uni.redirectTo({
						    url:"/pages/login/login"
						})
					}
				}
			})
		} else if (res.data.error === 'INVALID_TOKEN') {
			uni.showModal({
				title: '提示',
				content: res.data.msg,
				showCancel:false,
				success: res => {
					if (res.confirm) {
						console.log(res)
						uni.redirectTo({
						    url:"/pages/login/login"
						})
					}
				}
			})
		} else if (res.data.error === 'CANT_COLLECT_BATCH') {
			uni.showModal({
				title: '提示',
				content: '普通揽收不可揽收批次运单，是否跳转批次揽收？',
				showCancel: true,
				success: re => {
					if (re.confirm) {
						uni.navigateTo({
							url: `/pages/batchCollectResult/batchCollectResult?code=${res.data.msg}`
						})
					}
				}
			})
		}else {
			uni.showModal({
				title: '提示',
				content: res.data.msg,
				showCancel:false
			})
		}
	}
	
}, function(error) {
	console.log('返回进入拦截失败')
	return Promise.reject(error);
});
 
export default uniRequest